package com.socool.sinofaith.ui;

import java.util.ArrayList;
import java.util.Date;

import net.tsz.afinal.FinalHttp;
import net.tsz.afinal.http.AjaxCallBack;
import net.tsz.afinal.http.AjaxParams;

import com.socool.sinofaith.R;
import com.socool.sinofaith.SocoolApplication;
import com.socool.sinofaith.dao.QuestionAnswerDao;
import com.socool.sinofaith.dao.SifQuestionOptionsDao;
import com.socool.sinofaith.dao.SifSubTaskDao;
import com.socool.sinofaith.dao.SifSubTemplateDao;
import com.socool.sinofaith.dao.SifTaskAddressDao;
import com.socool.sinofaith.dao.SifTaskDao;
import com.socool.sinofaith.dao.SifTaskQuestionDao;
import com.socool.sinofaith.jsonparse.HttpJsonAdapter;
import com.socool.sinofaith.model.BaseMessage;
import com.socool.sinofaith.model.SifSubTaskBean;
import com.socool.sinofaith.model.SifTaskBean;
import com.socool.sinofaith.util.ConstansJsonUrl;
import com.socool.sinofaith.util.DateUtils;
import com.socool.sinofaith.util.SinofaithUtil;
import com.socool.sinofaith.view.TiPsDialog;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

/**
 * TaskFragment 
 * @author YD
 *
 */
public class MyTaskFragment extends Fragment implements View.OnClickListener {

	
	private Button currentTaskButton, runningTaskButton;
	private CurrentTaskFragment mCurrentTaskFragment;
	private RuningTaskFragment mRuningTaskFragment;
	private FragmentManager fragmentManager;
	private TiPsDialog mTiPsDialog;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.fragment_mytask, null);
		SocoolApplication.addActivity(this.getActivity());
		mTiPsDialog = new TiPsDialog(this.getActivity(), R.style.dialog, null,
				"离线登录状态下该功能不可用");
		currentTaskButton = (Button) view.findViewById(R.id.currentTaskButton);
		runningTaskButton = (Button) view.findViewById(R.id.runningTaskButton);
		currentTaskButton.setOnClickListener(this);
		runningTaskButton.setOnClickListener(this);
		mCurrentTaskFragment = new CurrentTaskFragment();
		mRuningTaskFragment = new RuningTaskFragment();
//		deteleDelay();
		fragmentManager = this.getFragmentManager();
		if (!((SocoolApplication) MyTaskFragment.this.getActivity()
				.getApplication()).isOffLineLogin()) {
			FragmentTransaction transaction = fragmentManager
					.beginTransaction();
			transaction.replace(R.id.mytaskcontent, mCurrentTaskFragment);
			transaction.commit();
		} else {

			runningTaskButton.setBackgroundColor(this.getActivity()
					.getResources().getColor(R.color.actionbarcolor));
			currentTaskButton.setBackgroundColor(this.getActivity()
					.getResources().getColor(R.color.taskbtncolor));
			currentTaskButton.setTextColor(this.getActivity().getResources()
					.getColor(R.color.black));
			runningTaskButton.setTextColor(this.getActivity().getResources()
					.getColor(R.color.white));
			FragmentTransaction transaction = fragmentManager
					.beginTransaction();
			transaction.replace(R.id.mytaskcontent, mRuningTaskFragment);
			transaction.commit();
		}

		return view;
	}

	@Override
	public void onClick(View arg0) {
		switch (arg0.getId()) {

		case R.id.currentTaskButton:
			if (!((SocoolApplication) MyTaskFragment.this.getActivity()
					.getApplication()).isOffLineLogin()) {
				currentTaskButton.setBackgroundColor(this.getActivity()
						.getResources().getColor(R.color.actionbarcolor));
				runningTaskButton.setBackgroundColor(this.getActivity()
						.getResources().getColor(R.color.taskbtncolor));
				currentTaskButton.setTextColor(this.getActivity()
						.getResources().getColor(R.color.white));
				runningTaskButton.setTextColor(this.getActivity()
						.getResources().getColor(R.color.black));
				FragmentTransaction transaction1 = fragmentManager
						.beginTransaction();
				transaction1.replace(R.id.mytaskcontent, mCurrentTaskFragment);
				transaction1.commit();
			} else {
				mTiPsDialog.show();
			}
			break;

		case R.id.runningTaskButton:

			if (!((SocoolApplication) MyTaskFragment.this.getActivity()
					.getApplication()).isOffLineLogin()) {
				runningTaskButton.setBackgroundColor(this.getActivity()
						.getResources().getColor(R.color.actionbarcolor));
				currentTaskButton.setBackgroundColor(this.getActivity()
						.getResources().getColor(R.color.taskbtncolor));
				currentTaskButton.setTextColor(this.getActivity()
						.getResources().getColor(R.color.black));
				runningTaskButton.setTextColor(this.getActivity()
						.getResources().getColor(R.color.white));
				FragmentTransaction transaction2 = fragmentManager
						.beginTransaction();
				transaction2.replace(R.id.mytaskcontent, mRuningTaskFragment);
				transaction2.commit();
			}
			break;

		}

	}
	
	private void deteleDelay(){
		SifTaskDao taskDao=new SifTaskDao(getActivity());
		QuestionAnswerDao questionAnswerDao = new QuestionAnswerDao(getActivity());
		SifQuestionOptionsDao sifQuestionOptionsDao = new SifQuestionOptionsDao(getActivity());
		SifSubTaskDao sifSubTaskDao = new SifSubTaskDao(getActivity());
		SifSubTemplateDao sifSubTemplateDao = new SifSubTemplateDao(getActivity());
		SifTaskAddressDao sifTaskAddressDao = new SifTaskAddressDao(getActivity());
		SifTaskQuestionDao sifTaskQuestionDao = new SifTaskQuestionDao(getActivity());
		String sql="select * from Sif_Task";
		ArrayList<SifTaskBean> arrayList = taskDao.queryBySql(sql);
		for(SifTaskBean bean:arrayList){
			Log.i("TAG", bean.getModifyTime()+"  modifytime");
			if(bean.getModifyTime()!=null){
				Date date=DateUtils.strToDateLong(bean.getModifyTime());
				Date now=new Date();
				if(isOutOff(date, now)){
					String taskId=bean.getTask_ID();
					questionAnswerDao.deleteByTaskId(taskId);
					taskDao.deleteByTaskId(taskId);
					sifQuestionOptionsDao.deleteByTaskId(taskId);
					sifSubTaskDao.deleteByTaskId(taskId);
					sifSubTemplateDao.deleteByTaskId(taskId);
					sifTaskAddressDao.deleteByTaskId(taskId);
					sifTaskQuestionDao.deleteByTaskId(taskId);
				}
			}
		}
		
		
	}
	
	private boolean isOutOff(Date after,Date now){
		if((now.getTime()-after.getTime())>24*2*60*60)
//		if((now.getTime()-after.getTime())>60000)
			return true;
		return false;
		
	}
	private String getSPString(){
		SharedPreferences sp=getActivity().getSharedPreferences("login", Context.MODE_PRIVATE);
		return sp.getString("UserMobile", "");
	}
	
	

}
